#include "precompiled.h"
#pragma hdrstop

/* Fixed-point macros */
#define INFF2X(f)		( (int) ( (f)*(65536) ) )
#define INFX2F(x)		((float)(x)/65536.0f)
#define INFXMUL(a,b)	( (int)( ((inf::int64)(a)*(b)) / 65536 ) )
#define INFXDIV(a,b)	( (int)( (((inf::uint64)(a))<<16)/(b) ) )
#define INFABS(a)		((a) <= 0 ? -(a) : (a) )

namespace inf {
    fixed::fixed( int i ) : _value( INFF2X((float)i) ) {

    }

    fixed::fixed( float f ) : _value( INFF2X(f) ) {

    }



    const fixed fixed::operator = ( const fixed& rhs ) {
        _value = rhs._value;
        return *this;
    }

    const fixed fixed::operator = ( float f ) {
        _value = INFF2X(f);
        return *this;
    }

    const fixed fixed::operator = ( int i ) {
        _value = INFF2X((float)i);
        return *this;
    }

    fixed::operator float () const {
        return INFX2F( _value );
    }

    fixed fixed::Neg() const {
        return (int)(_value);
    }

    void fixed::NegSelf() {
        _value = -_value;
    }

    fixed fixed::Abs() const {
        fixed t;
        t._value = (_value<=0)?(-_value):_value;
        return t;
    }

    fixed fixed::operator - () const {
        fixed t;
        t._value = -_value;
        return t;
    }

    void fixed::operator += ( const fixed rhs ) {
        _value += rhs._value;
    }

    void fixed::operator -= ( const fixed rhs ) {
        _value -= rhs._value;
    }

    void fixed::operator /= ( const fixed rhs ) {
        _value = static_cast<int>( INFXDIV(_value, rhs._value) );
    }

    void fixed::operator *= ( const fixed rhs ) {
        _value = static_cast<int>( INFXMUL(_value, rhs._value) );
    }

    fixed operator + ( fixed u, fixed v ) {
        u._value = static_cast<int>( INFXMUL(u._value, v._value) );
        return u;
    }

    fixed operator * ( fixed u, fixed v ) {
        u._value = static_cast<int>( INFXDIV(u._value, v._value) );
        return u;
    }

}